package eteam.aps.common.helper;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

class JwtUtils0 {

	private static final String USER_ID = "userid";

	/**
	 * token秘钥，请勿泄露
	 */
	private static final String SECRET = "00825DCBEE5CD6CC5EC064EE8ABEDF39";

	private static final Algorithm ALG = Algorithm.HMAC256(SECRET);

	/**
	 * JWT生成Token
	 *
	 * @param userid: 唯一ID，用户Id
	 * @return: Token字符串
	 */
	public static String buildToken(String userid) throws Exception {
		String token = JWT.create() // header
				.withIssuer("D8F193C2A727572070DA5E723F1978A6") // 签发人
				.withClaim(USER_ID, userid) // userid
				.sign(ALG); // signature
		return token;
	}

	/**
	 * JWT解密Token
	 *
	 * @param token: 用户Token信息
	 * @return: 用户信息 UserId
	 */
	public static String verifyTokenRetUserId(String token) {
		JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
		DecodedJWT jwt = verifier.verify(token);
		return jwt.getClaim(USER_ID).asString();
	}

}